<?php

require_once("../function/connection.php");

class validator {

    public $iValidationError = 0;

    public function validateEmail($sEmail) {
        if ($this->isEmpty($sEmail) == false) {
            $this->iValidationError = 1;
            return false;
        }

        //email is not case sensitive make it lower case
        $sEmail = strtolower($sEmail);

        //check if email seems valid
        if (preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/", $sEmail)) {
            return $sEmail;
        } else {
            $this->iValidationError = 1;
            return false;
        }
    }

    public function validatePassword($sPassword, $sSecondPassword) {
        if ($this->isEmpty($sPassword) == false) {
            $this->iValidationError = 1;
            return false;
        }
        if ($this->isEmpty($sSecondPassword) == false) {
            $this->iValidationError = 1;
            return false;
        }

        if ($sPassword !== $sSecondPassword) {
            $this->iValidationError = 1;
            return false;
        } else {
            return $sPassword;
        }
    }

    public function validatePhone($iPhone) {
        if ($this->isEmpty($iPhone) == false) {
            $this->iValidationError = 1;
            return false;
        }
        if (!preg_match('/[1-9]{9}/', $iPhone)) {
            $this->iValidationError = 1;
            return false;
        } else {
            return $iPhone;
        }
    }

    public function isEmpty($mVariable) {
        stripslashes($mVariable);
        strip_tags($mVariable);
        if ($mVariable == "" || $mVariable == null) {
            $this->iValidationError = 1;
            return false;
        } else {
            return $mVariable;
        }
    }

    public function emailExist($sEmail) {

        connection();

        $query = "SELECT * from `user` where `USE_EMAIL` = '$sEmail';";
        $result = mysql_query($query);
        if( mysql_num_rows($result) == 0){
            return true;
        }else{
            $this->iValidationError = 1;
            return false;
        }
    }

}

?>
